﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BS_2.VTKClass
{
    class GGBAutoMap<K, V>
    {

        private int length = 320000;
        private List<Dictionary<K, V>> listData = new List<Dictionary<K, V>>();
        public GGBAutoMap()
        {
            listData.Add(getAutoMap());

        }

        public List<Dictionary<K, V>> getListMap()
        {
            return listData;
        }
        public bool ContainsKey(K key)
        {
            for (int i = 0; i < listData.Count; i++)
            {
                Dictionary<K, V> mapchid = listData[i];
                bool bo = mapchid.ContainsKey(key);
                if (bo)
                {
                    return bo;
                }
            }
            return false;
        }
        public V getValue(K key)
        {

            for (int i = 0; i < listData.Count; i++)
            {
                Dictionary<K, V> mapchid = listData[i];
                bool bo = mapchid.ContainsKey(key);
                if (bo)
                {
                    return mapchid[key];
                }
            }
            return default(V);
        }
        public void Add(K key, V val)
        {
            Dictionary<K, V> nowMap = getNowMap();
            nowMap.Add(key, val);
        }

        private Dictionary<K, V> getNowMap()
        {
            Dictionary<K, V> nowMap = listData[listData.Count - 1];
            int len = nowMap.Count;

            if (len < length)
            {
                return nowMap;
            }
            else
            {
                nowMap = getAutoMap();
                listData.Add(nowMap);
                return nowMap;
            }
        }

        private Dictionary<K, V> getAutoMap()
        {

            Dictionary<K, V> map = new Dictionary<K, V>(length);
            return map;
        }
    }
}
